import traceback,sys

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class MapTileList(QWidget):
    """A widget to hold a QListWidget with a list of tiles in the current Map's tileset."""
    def __init__(self, map):
        QWidget.__init__(self, None)
        self.vbox_outer = QVBoxLayout()
        self.tile_list_groupbox = QGroupBox("Tiles")
        self.vbox = QVBoxLayout()
        self.list = QListWidget()

        self.connect(self.list, SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.ondoubleclick)
        
        self.map = map

        self.buttonhboxcontainer = QWidget()
        self.buttonhbox = QHBoxLayout(self.buttonhboxcontainer)
        self.buttons = {}

        self.buttons["load_tile"] = QPushButton("+")
        self.buttons["clone_tile"] = QPushButton("Clone")
        self.buttons["new_tile"] = QPushButton("New")
        self.buttons["del_tile"] = QPushButton("-")

        for key,button in self.buttons.items():
            self.connect(button, SIGNAL("clicked()"), getattr(self,key))
            self.buttonhbox.addWidget(button)
            self.buttonhbox.setAlignment(button, Qt.AlignLeft)
        
        self.vbox.addWidget(self.list)
        self.vbox.addWidget(self.buttonhboxcontainer)

        self.tile_list_groupbox.setLayout(self.vbox)

        self.vbox_outer.addWidget(self.tile_list_groupbox)
        self.setLayout(self.vbox_outer)

        self.initialize_dialog()
        self.initialize_list()

    def load_tile(self):
        self.loadname = QFileDialog.getOpenFileName(self, "Load Sprite")
        self.map.add_tile(self.loadname)
        self.initialize_list()

    def clone_tile(self):
        clone = self.map.tile_set[self.list.currentRow()].clone()
        self.map.add_tile_from_sprite(clone)
        self.initialize_list()

    def new_tile(self):
        self.map.add_blank_tile()
        self.initialize_list()
        self.map.set_selected_tile_id(self.list.currentRow())
        self.map.open_sprite_editor(None)

    def del_tile(self):
        self.map.del_tile(self.list.currentRow())
        self.initialize_list()

    def initialize_dialog(self):
        self.connect(self.list, SIGNAL("currentRowChanged(int)"), self.row_changed)

    def initialize_list(self):
        """Clear and (re-)initialize list elements."""
        self.list.clear()
        for index,tile in enumerate(self.map.tile_set):
            item = QListWidgetItem("Tile " + str(index + 1))
            # Create an icon for this item.
            icon = QIcon(tile.frames[0].get_pixmap(True))
            item.setIcon(icon)
            item.setSizeHint(QSize(25,25))
            self.list.addItem(item)
        # Set current index to selected tile id.
        self.list.setCurrentRow(self.map.selected_tile_id, QItemSelectionModel.Select)

    def row_changed(self, ev):
        if self.list.currentRow() >= 0:
	        self.map.set_selected_tile_id(self.list.currentRow())

    def ondoubleclick(self, ev):
        self.map.set_selected_tile_id(self.list.currentRow())
        self.map.open_sprite_editor(ev)
